import dmPython
from common.conf_handle import myconf


class DB_Handler:
    """连接达梦数据库"""

    def __init__(self):

        self.conn = dmPython.connect(
            host=myconf.get("DM","host"),
            port=myconf.get("DM","port"),
            user=myconf.get("DM","user"),
            password=myconf.get("DM","password"))

        self.cursor = self.conn.cursor()

    def query(self, sql, args=None, one=False):
        """执行sql语句"""
        try:
            self.cursor.execute(sql, args)
            # 提交事务
            self.conn.commit()
            if one:
                return self.cursor.fetchone()
            else:
                return self.cursor.fetchall()
        except Exception as e:
            print(F"数据库查询失败：{e}")

    def close(self):
        """关闭数据库连接"""
        self.cursor.close()
        self.conn.close()


if __name__ == '__main__':
    DB = DB_Handler()
    datas=DB.query(f"SELECT ID,TITLE FROM LA_WEBSITE.SERVICE_GUIDE WHERE TITLE ='Demo_43'")
    print(datas)
    re1=datas[0][0]
    re2=datas[0][1]
    print(re1,re2)

    # for data in datas:
    #     print(data)
    # 数据库删除和插入预计不清楚能不能实现，

